package com.akeil.flash.preferences;

import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.akeil.flash.action.ReOpenSerialPortAction;

import flash.Activator;

public class SerialPortPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage{
	private ComboFieldEditor port;
	private ComboFieldEditor baud;
	private ComboFieldEditor databits;
	private ComboFieldEditor parity;
	private ComboFieldEditor stopbits;
	

	public SerialPortPreferencePage(){
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}
	
	protected void createFieldEditors() {
		Composite parent = getFieldEditorParent();
        
		port = new ComboFieldEditor(IPreferenceConstants.SERIAL_PORT, "通信口", new String[][]{
				{"COM1","COM1"},{"COM2","COM2"},{"COM3","COM3"},{"COM4","COM4"},{"COM5","COM5"},{"COM6","COM6"},{"COM7","COM7"},{"COM8","COM8"},{"COM9","COM9"},{"COM10","COM10"},
				{"COM11","COM11"},{"COM12","COM12"},{"COM13","COM13"},{"COM14","COM14"},{"COM15","COM15"},{"COM16","COM16"},{"COM17","COM17"},{"COM18","COM18"},{"COM19","COM19"},{"COM20","COM20"},
				{"COM21","COM21"},{"COM22","COM22"},{"COM23","COM23"},{"COM24","COM24"},{"COM25","COM25"},{"COM26","COM26"},{"COM27","COM27"},{"COM28","COM28"},{"COM29","COM29"},{"COM30","COM30"}}, 
				parent);
		addField(port);

		baud = new ComboFieldEditor(IPreferenceConstants.SERIAL_BAUD, "波特率", new String[][]{{"115200","115200"},{"57600","57600"},{"38400","38400"},{"19200","19200"},{"9600","9600"},{"4800","4800"}}, 
				parent);
		addField(baud);
		
		databits = new ComboFieldEditor(IPreferenceConstants.SERIAL_DATABITS, "数据位", new String[][]{{"8","8"},{"7","7"},{"6","6"},{"5","5"}}, 
				parent);
		addField(databits);
		
		parity = new ComboFieldEditor(IPreferenceConstants.SERIAL_PARITY, "校验位", new String[][]{{"无校验","None"},{"奇校验","Odd"},{"偶校验","Even"},{"屏蔽","Mark"},{"空格","Space"}}, 
				parent);
		addField(parity);
		
		stopbits = new ComboFieldEditor(IPreferenceConstants.SERIAL_STOPBITS, "停止位", new String[][]{{"1","1"},{"1.5","1.5"},{"2","2"}}, 
				parent);
		addField(stopbits);
	}

	@Override
	protected void performDefaults() {
		port.loadDefault();
		baud.loadDefault();
		databits.loadDefault();
		parity.loadDefault();
		stopbits.loadDefault();
	}

	@Override
	public boolean performOk() {
		boolean ret =  super.performOk();
		performApply();
		return ret;
	}

	@Override
	protected void performApply() {
		new ReOpenSerialPortAction().run();
	}

	@Override
	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub
		
	}

}
